/*
 * Created on Feb 11, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.query.project;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;

import edu.mit.simile.longwell.Profile;

/**
 * @author dfhuynh
 */
public class URIProjector extends PropertyProjector {
	protected class URIPropertyProjection 
		extends PropertyProjection 
		implements IURIProjection {
		
		public URIPropertyProjection(Set objects) {
			super(
				URIProjector.this.m_profile, 
				URIProjector.this.m_property, 
				URIProjector.this.m_forward, 
				URIProjector.this.m_locale, 
				objects);
		}

		/* (non-Javadoc)
		 * @see edu.mit.simile.longwell.query.project.PropertyProjection#internalGetObjects(java.lang.Object)
		 */
		protected Set internalGetObjects(Object value) {
			Set		allObjects = getObjects();
			Set 	objects = new HashSet();
			URI		uri = (URI) value;
			
			Iterator i = allObjects.iterator();
			while (i.hasNext()) {
				Resource r = (Resource) i.next();
				if (urisEqual(uri, (URI) getValue(r))) {
					objects.add(r);
				}
			}
			return objects;
		}
		
		/* (non-Javadoc)
		 * @see edu.mit.simile.longwell.query.project.PropertyProjection#nodeToValue(com.hp.hpl.jena.rdf.model.RDFNode)
		 */
		protected Object nodeToValue(RDFNode node) {
			return nodeToURI(node);
		}

		/* (non-Javadoc)
		 * @see edu.mit.simile.longwell.query.project.IURIProjection#getURI(com.hp.hpl.jena.rdf.model.Resource)
		 */
		public URI getURI(Resource object) {
			return (URI) getValue(object);
		}

		/* (non-Javadoc)
		 * @see edu.mit.simile.longwell.query.project.ProjectionBase#internalGetObjectToValueMap()
		 */
		protected Map internalGetObjectToValueMap() {
			Map m = new HashMap();
			fillObjectToValueMap(m);
			return m;
		}

		@Override
		public String getProperty() {
			// TODO Auto-generated method stub
			return null;
		}
	}
	
	/**
	 * @param profile
	 * @param parameter
	 */
	public URIProjector(Profile profile, String parameter, String locale) {
		super(profile, parameter, locale);
	}
	
	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.project.PropertyProjector#isEfficientForRootProjection()
	 */
	public boolean isEfficientForRootProjection() {
		return false;
	}
	
	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.project.ProjectorBase#internalProject(java.util.Set)
	 */
	protected IProjection internalProject(Set objects) {
		return new URIPropertyProjection(objects);
	}
	
	final protected boolean urisEqual(URI uri1, URI uri2) {
		if (uri1 == null) {
			return uri2 == null;
		} else {
			return uri1.equals(uri2);
		}
	}
	
	final protected URI nodeToURI(RDFNode node) {
		String	s = null;
		
		if (node instanceof Literal) {
			s = ((Literal) node).getString();
		} else if (node instanceof Resource) {
			s = m_profile.getSchemaModel().resourceToPseudoURI((Resource) node);
		}
		
		if (s != null) {
			try {
				return new URI(s);
			} catch (URISyntaxException e) {
			}
		}
		return null;
	}
}
